<template>
    <div class="detail-layout">
        <div class="detail-main">
            <header class="header">
                <div class="left">
                    <img src="/src/assets/logo.png" alt="logo" width="150" @click="goHome">
                    <div class="search-bar-wrap search-bar-light pc-header-search">
                        <div class="adm-input search-bar-input"><input class="adm-input-element" v-model="searchText"
                                placeholder="Enter your keywords" @keyup.enter="handleSearch"></div><span
                            class="search-icon" @click="handleSearch"></span>
                    </div>
                </div>
                <div class="pc-header-extra">
                    <div class="dropdown pc-header-extra-item">
                        <div class="dropdown-title"><span>Categories</span></div>
                        <div class="dropdown-list">
                            <p class="dropdown-list-item" @click="goArticle(item)" v-for="(item, index) in category"
                                :key="index">{{ item?.alias }}</p>
                        </div>
                    </div><span class="dropdown-title" @click="showFeedBack"><span
                            class="pc-feedback-text">Feedback</span></span>
                </div>
            </header>

            <main class="main">
                <div class="main-page">
                    <div style="display: flex; flex-direction:column; gap: 15px;">
                        <div class="main-article">
                            <h1 class="multi-ellipsis-base detail-page-title">{{ detailData?.title }}</h1>
                            <p style="font-size: 16px; margin-bottom: 8px; line-height: 22px;">{{ detailData?.firstP }}
                            </p>
                            <div id="relatedsearches1" style="min-height: 400px;"></div>
                            <div style="width: 100%;  overflow: hidden; margin-bottom: 8px;">
                                <p style="font-size: 16px;  line-height: 22px;" v-html="detailData?.text"></p>
                            </div>

                        </div>
                        <div id="relatedsearches2" style="min-height: 400px;max-width: 765px;"></div>
                    </div>
                    <RecentPost />
                </div>
            </main>

            <footer class="footer">
                <p><a style="text-decoration: none;color: #aaa;" href="/articles/769.html">Imprint</a> |
                    <a style="text-decoration: none;color: #aaa;" href="/articles/768.html">Privacy Policy</a> |
                    <a style="text-decoration: none;color: #aaa;" href="/articles/767.html">Terms of Service</a> |
                    <a style="text-decoration: none;color: #aaa;" href="/articles/770.html">Contact Us</a>
                </p>
                <p style="margin-top: 12px;">© 2024 - 2025 OhWellMind All Rights Reserved.</p>
            </footer>
        </div>


    </div>
</template>

<script setup lang="ts">
import { computed, defineComponent, onMounted, ref, watch } from 'vue'
defineComponent({ name: 'Detail' })
import { useRoute, useRouter } from 'vue-router'
import { getAllCategory, getArticleDetail } from '../api/api';
import RecentPost from '../components/recentPost.vue'
import { ElMessage, ElMessageBox } from 'element-plus';
const router = useRouter()
const route = useRoute()
const searchText = ref<string>('')
const category = ref([])
const detailData = ref()
const terms = ref('')
const goHome = () => {
    router.push('/')
}

const goArticle = (item) => {
    router.push({
        name: 'Article',
        params: { id: item?.id },
        query: { title: item.alias }
    })
}

const handleSearch = () => {
    if (!searchText.value) return
    router.push({
        name: 'SearchPage',
        query: { search: searchText.value }
    })
}
const getCategory = () => {
    getAllCategory()
        .then(response => {
            category.value = response.data?.filter((item) => item.id === 108 || item.id === 111 || item.id === 112 || item.id === 113 || item.id === 120 || item.id === 121)
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
}

const showFeedBack = () => {
    ElMessageBox.prompt('Please input your feedback', 'Feedback', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
    })
        .then(({ value }) => {
            ElMessage({
                type: 'success',
                message: `THANK YOU`,
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: 'Input canceled',
            })
        })
}

const rsblock1 = {
    "container": "relatedsearches1",
    "relatedSearches": 7
};

const rsblock2 = {
    "container": "relatedsearches2",
    "relatedSearches": 5
};

const CHANNEL = route.query?.['channel'] || '3628479155';
const PIXEL = route.query?.['tt_pixel_id'] || 'CVLNPM3C77UCTQ7C57IG';
const KW = route.query?.['ref'] || '';
const hit = (route.query['hit'] as string)?.replace(/^{|}$/g, '') || '';
let url = `https://ohwellmind.com/searchPage?channel=${CHANNEL}&pixelId=${PIXEL}&hit=${hit}`;
const pageOptions = ref({
    "pubId": "partner-pub-9126156863543510",
    "relatedSearchTargeting": "content",
    "adsafe": 'low',
    "adPage": 1,
    "adtest": 'off',
    "ie": 'utf-8',
    "oe": 'utf-8',
    "styleId": "9415107969",
    "resultsPageBaseUrl": url,
    "resultsPageQueryParam": "search",
    "ignoredPageParams": "ref,channel,campaignid,afc,gcild,ttclid,bbg,pixelId,pi_source,tt_pixel_id,vid,hit",
    'terms': terms.value || '',
    'referrerAdCreative': KW,
    "channel": CHANNEL,
    "ivt": false
})

const onMessage = (e) => {
    if ("string" == typeof e.data && e.data.match("FSXDC,.aCS")) {
        if ("https://www.google.com" === e.origin ||
            "https://www.adsensecustomsearchads.com" === e.origin ||
            "https://syndicatedsearch.goog" === e.origin) {
            console.log("Search");
            // @ts-ignore
            ttq.track('Search');
        }
    }
};
window.addEventListener("message", onMessage);

const getDetailData = () => {
    return getArticleDetail({ id: route?.params?.id as string || null }) // 显式 return
        .then(response => {
            detailData.value = response?.data;
            pageOptions.value.terms = response.data.terms || '';
            pageOptions.value.resultsPageBaseUrl = `https://ohwellmind.com/searchPage?channel=${CHANNEL}&pixelId=${PIXEL}&hit=${hit}&article_id=${route.params?.id}&title=${encodeURIComponent(response.data?.title) || ''}`
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
}

onMounted(async () => {
    getCategory()
    await getDetailData()
    document.body.style.backgroundColor = '#effdff'
    const payload = JSON.parse(JSON.stringify(pageOptions.value));
    // @ts-ignore
    _googCsa?.('relatedsearch', payload, rsblock1, rsblock2);
})

</script>

<style lang="scss">
.detail-layout {
    background-color: #effdff;
    min-height: 100vh;


    .detail-main {
        max-width: 1200px;
        margin: auto;


        .header {
            height: 100px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 24px 20px;
            text-align: center;
            color: #fff;

            .search-bar-light {
                background-color: #fff;
                border: 2px solid #fff;
            }

            .search-bar-wrap {
                display: flex;
                align-items: center;
                width: calc(100% - 6px);
                max-width: 748px;
                height: 44px;
                padding: 9px 12px;
                margin: 40px auto 30px;
                border-radius: 10px;
            }

            .search-bar-light .adm-input-element {
                color: #333;
            }

            .pc-header-search {
                display: none;
                width: 482px;
                margin: 0 0 0 22px;

                .adm-input {
                    --font-size: var(--adm-font-size-9);
                    --color: var(--adm-color-text);
                    --placeholder-color: var(--adm-color-light);
                    --text-align: left;
                    --background-color: transparent;
                    display: flex;
                    justify-content: flex-start;
                    align-items: center;
                    width: 100%;
                    max-width: 100%;
                    max-height: 100%;
                    min-height: 24px;
                    background-color: var(--background-color);

                }

                .adm-input-element {
                    flex: auto;
                    display: inline-block;
                    box-sizing: border-box;
                    width: 100%;
                    max-width: 100%;
                    max-height: 100%;
                    padding: 0;
                    margin: 0;
                    font-size: var(--font-size);
                    line-height: 1.5;
                    background: transparent;
                    border: 0;
                    outline: none;
                    -webkit-appearance: none;
                    appearance: none;
                    min-height: 1.5em;
                    text-align: var(--text-align);
                    font-style: italic;
                }


                .search-bar-input {
                    height: 30px;
                    line-height: 30px;
                    font-size: 16px;
                    border: none;
                }

                .search-icon {
                    width: 18px;
                    height: 18px;
                    margin-left: 6px;
                    background: no-repeat url(https://6gamehub.com/static/img/niceranker/assets/search-icon.webp);
                    background-size: 18px 18px;
                    cursor: pointer;
                }

            }




            .left {
                display: flex;
                align-items: center;
                gap: 10px;
            }



            .pc-header-extra {
                display: none;

                .dropdown-title {
                    padding: 6px 10px;
                    background: rgba(0, 0, 0, .3);
                    border-radius: 8px;
                    line-height: 18px;
                    cursor: pointer;
                    color: #fff;
                }

                .pc-header-extra-item {
                    margin-right: 20px;
                }

                .pc-header-extra-item .dropdown-list {
                    min-width: 120px;
                }

                .pc-header-extra-item .dropdown-list-item {
                    text-transform: capitalize;
                }

                .dropdown {
                    position: relative;
                    cursor: pointer;

                    .dropdown-list {
                        overflow: hidden;
                        position: absolute;
                        display: none;
                        width: 100%;
                        height: 0;
                        left: -8px;
                        z-index: 999;
                        padding-top: 10px;
                        color: #fff;
                        background: transparent;
                        transition: height 1s;
                        border-radius: 0 0 8px 8px;

                        .dropdown-list-item {
                            padding: 12px;
                            text-align: center;
                            font-size: 16px;
                            line-height: 16px;
                            cursor: pointer;
                            background-color: rgba(128, 128, 128, 0.6);
                        }

                        .dropdown-list-item:hover {
                            background-color: #246def;
                        }

                        .dropdown-list-item:first-child {
                            border-radius: 8px 8px 0 0;
                        }
                    }
                }

                .dropdown:hover .dropdown-list {
                    display: block;
                    height: auto;
                }
            }

            .pc-feedback {
                cursor: pointer;
            }


            @media screen and (min-width: 765px) {
                .pc-header-search {
                    display: flex;
                }

                .pc-header-extra {
                    display: flex;
                }
            }


        }

        @media screen and (max-width: 765px) {
            .header {
                justify-content: center;
            }
        }

        .main {

            padding: 10px 20px;
            min-height: calc(100vh - 190px);

            h1,
            h2,
            li,
            ol,
            p,
            ul {
                margin: 0;
                padding: 0;
                font-family: "exo", Sans-Serif !important
            }

            h2,
            h3 {
                display: block;
                margin-block-start: 1em;
                margin-block-end: 1em;
                margin-inline-start: 0px;
                margin-inline-end: 0px;
                font-weight: bold;
                unicode-bidi: isolate;
                font-family: "exo", Sans-Serif !important
            }


            li {
                font-size: 16px;
                line-height: 24px;
                margin-bottom: 16px;
                list-style: disc;
                margin-left: 18px;
            }

            p {
                font-size: 16px;
                font-weight: 400;
                color: #333;
                line-height: 24px;
                margin-bottom: 16px;
                font-family: "exo", Sans-Serif !important
            }

            table {
                border-spacing: 0;
                border: 1px solid #ddd;
                border-right: none;
                border-bottom: none;
                margin-bottom: 16px;

                td {
                    border-right: 1px solid #ddd;
                    border-bottom: 1px solid #ddd;
                    padding: 14px 24px;
                }

                th {
                    border-right: 1px solid #ddd;
                    border-bottom: 1px solid #ddd;
                    background-color: #cbcbcb;
                }
            }

            span {
                font-family: "exo", Sans-Serif !important
            }


            .main-page {
                .main-article {
                    max-width: 765px;
                    min-width: 300px;

                    img {
                        max-width: 100% !important;
                        height: auto !important;
                        display: block;
                    }

                    .multi-ellipsis-base {
                        overflow: hidden;
                        display: -webkit-box;
                        text-overflow: ellipsis;
                        -webkit-box-orient: vertical;
                    }

                    .detail-page-title {
                        position: relative;
                        -webkit-line-clamp: 4;
                        font-weight: 800;
                        font-size: 26px;
                        color: #333;
                        line-height: 30px;
                        margin-bottom: 8px;
                    }


                }

            }

            @media screen and (min-width: 765px) {
                .main-page {
                    display: flex;
                    justify-content: space-between;
                    gap: 15px;

                }
            }


        }

        .footer {
            color: #aaa;
            margin: 0 auto;
            padding: 20px 0;
            font-size: 14px;
            line-height: 13px;
            text-align: center;
        }
    }
}
</style>